// componnets/navigateBar.js
import store from '../../modules/store'
import tool from '../../modules/tool'
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    currentPageIndex: -1,
    status: 1,
    unreadNum: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleAllowLongTap() {
      store.setItem('homeLongTap', true)
      setTimeout(() => {
        store.removeItem('homeLongTap')
      }, 10 * 1000);
    },
    handleChange(e) {
      if (e.currentTarget.dataset.index == 4 || e.currentTarget.dataset.index == 2) {
        if (!store.getItem('userData')) {
          tool.handleNotLogin()
          return
        }
      }
      if (this.data.currentPageIndex == e.currentTarget.dataset.index) return
      tool.postFrontStatus(`首页-${(() => {
        if (e.currentTarget.dataset.index == 0) return '首页'
        if (e.currentTarget.dataset.index == 1) return '巡查'
        if (e.currentTarget.dataset.index == 2) return '通知'
        if (e.currentTarget.dataset.index == 4) return '我的'
        if (e.currentTarget.dataset.index == 5) return '统计'
      })()}`)
      wx.vibrateShort()
      store.setItem('mainCurrentPageIndex', e.currentTarget.dataset.index)
    }
  },

  created() {
    let data = store.getItem('mainCurrentPageIndex')
    this.setData({
      currentPageIndex: data
    })
    store.onChange('mainCurrentPageIndex', (e) => {
      let data = store.getItem('mainCurrentPageIndex')
      this.setData({
        currentPageIndex: data
      })
    })
    store.onChange('navigateBarStatus', () => {
      this.setData({
        status: store.getItem('navigateBarStatus')
      })
    })

    this.setData({ unreadNum: tool.store.getItem('unreadNum') })
    tool.store.onChange('unreadNum', () => this.setData({ unreadNum: tool.store.getItem('unreadNum') }))
  }
})